package com.tingyun.sign.questions.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tingyun.sign.activity.service.ActivityService;
import com.tingyun.sign.companyUser.service.CompanyUserService;
import com.tingyun.sign.core.entity.ScActivityBean;
import com.tingyun.sign.core.entity.ScQuestionsBean;
import com.tingyun.sign.core.entity.ScUCompanyUserBean;
import com.tingyun.sign.core.entity.ScUManagerBean;
import com.tingyun.sign.questions.service.QuestionsService;
import com.tingyun.sign.utils.BaseController;
import com.tingyun.sign.utils.DateUtils;
import com.tingyun.sign.utils.Page;
//import com.tingyun.sign.utils.PaginationParams;

@Controller
@RequestMapping("questions")
public class QuestionsController extends BaseController{

	@Autowired
	private QuestionsService questionsService;
	
	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private CompanyUserService companyUserService;

	@RequestMapping(value="index")
	public ModelAndView index(ScQuestionsBean bean,HttpSession session){
		bean.setStartTimeStr(DateUtils.converToStringDate(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd"));
		bean.setEndTimeStr(DateUtils.converToStringDate(new Date(),"yyyy-MM-dd"));
		ModelAndView mv = new ModelAndView();
//		List<ScActivityBean> list = new ArrayList<ScActivityBean>();
//		ScActivityBean b = new ScActivityBean();
//		b.setId(12L);
//		b.setTopic("活动1");
//		list.add(b);
		//根据manager权限，显示不同的activity
		ScUManagerBean manager = getSessionUser(session);
		List<ScActivityBean> activityBean;
//		if(manager.getType() == 0){
			activityBean = activityService.findAllActivities(getSessionUser(session).getType());	
		/*}
		else{
			activityBean = questionsService.findActivitiesByArea(manager.getType());
		}*/
		mv.addObject("acs", activityBean);
		mv.addObject("obj",bean);
//		mv.addObject("acs", list);
//		mv.setViewName("questions/findQuestionsAndAnswer");
		mv.setViewName("questions/questionList");
		return mv;
	}
	
	@RequestMapping(value = "list")
	@ResponseBody
	public Map<String, Object> list(ScQuestionsBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
 		Page<ScQuestionsBean> page = questionsService.findQuestionsPage(bean);
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResultList());
		return map;
	}
	
	/*//在提问页面下面的已提问题列表
	@RequestMapping(value = "listForAsk")
	@ResponseBody
	public Map<String, Object> listForAsk(ScQuestionsBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
 		Page<ScQuestionsBean> page = questionsService.listQuestions(bean);
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResultList());
		return map;
	}*/
	
	//在提问页下方显示本次活动已提问题
	@RequestMapping(value = "listQuestions")
	public ModelAndView listQuestions(ScQuestionsBean scQuestionsBean){
		ModelAndView modelAndView = new ModelAndView();
		List<ScQuestionsBean> listQuestions=questionsService.listQuestions(scQuestionsBean);
		//查询客户信息
		ScUCompanyUserBean companyUserBean = companyUserService.findCompanyUser(scQuestionsBean.getCompanyUserId());
		modelAndView.addObject("listQuestions", listQuestions);
		modelAndView.addObject("companyUserBean", companyUserBean);
		modelAndView.setViewName("questions/askQuestionPage");
//		Map<String, Object> map = new HashMap<String, Object>();
//		Page<ScQuestionsBean> page = questionsService.listQuestions(bean);
//		map.put("total", page.getTotalCount());
//		map.put("rows", page.getResultList());
		return modelAndView;
	}
	
/*	//在提问页下方显示本次活动已提问题
		@RequestMapping(value = "listQuestions")
		@ResponseBody
		public Map<String, Object> listQuestions(ScQuestionsBean scQuestionsBean) {
			List<ScQuestionsBean> listQuestions=questionsService.listQuestions(scQuestionsBean);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("listQuestions", listQuestions);
//			request.setAttribute("listQuestions", listQuestions );
			return map;
		}*/
	/*
	添加问题
	*/
	@RequestMapping(value = "addQuestion")
	@ResponseBody
	public Map<String, Object> addQuestions(HttpSession session, ScQuestionsBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = null;
		try {
//			ScUCompanyUserBean companyUserBean;
			ScUManagerBean manager = getSessionUser(session);
			bean.setCuser(manager.getId());
			bean.setMuser(manager.getId());
			bean.setStatus(0);
//			bean.setActivityId(bean.getActivityId());
			result = questionsService.addQuestions(bean);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	//回复问题
	@RequestMapping(value = "answerQuestion")
	@ResponseBody
	public Map<String, Object> answerQuestion(HttpSession session, ScQuestionsBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = null;
		try {
			ScUManagerBean manager = getSessionUser(session);
			bean.setMuser(manager.getId());
			result = questionsService.answerQuestion(bean);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}
	
	//查看回复
	@RequestMapping("showAnswer")
	@ResponseBody
	public Map<String, Object> showAnswer(HttpSession session, long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = null;
		try {
			ScQuestionsBean question=questionsService.getQuestion(id);
			System.out.println(id);
			if(question != null) result=1;
			else result =0;
			map.put("question", question);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}
	
	/**
	 * 跳转到客户列表页，提问按钮
	 */
	@RequestMapping(value = "companyUserList")
	public ModelAndView companyUserList(HttpSession session, ScUCompanyUserBean bean) {
		bean.setStartTimeStr(DateUtils.converToStringDate(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd"));
		bean.setEndTimeStr(DateUtils.converToStringDate(new Date(),"yyyy-MM-dd"));
		ModelAndView mv = new ModelAndView();
		List<ScActivityBean> acs = activityService.findAllActivities(getSessionUser(session).getType());
		mv.addObject("obj",bean);
		mv.addObject("acs",acs);
		mv.setViewName("questions/addQuestion");
		return mv;
	}
	
}
